/*******************************************************************************
 * tuannh ********** MAP ACTION
 ******************************************************************************/
MapAction = {
		
	DeActive : function() {
		var i = 0;
		if (pan_map != undefined)
			pan_map.deactivate();
		for (i = 0; i < Draw_Obj.length; i++) {
			Draw_Obj[i].deactivate();
		}
		for (i = 0; i < Modify_Obj.length; i++) {
			Modify_Obj[i].deactivate();
		}
		for (i = 0; i < Del_Obj.length; i++) {
			Del_Obj[i].deactivate();
		}
		for (i = 0; i < DelVertices_Obj.length; i++) {
			DelVertices_Obj[i].deactivate();
		}

		if (draw_Zoom != null)
			draw_Zoom.deactivate();		
		if (split != null)
			split.deactivate();
		if (clickEvent != null)
			clickEvent.deactivate();
		if (ctrlSelectFeatures != null)
			ctrlSelectFeatures.deactivate();
		for ( var key in measureControls) {
			control = measureControls[key];
			control.deactivate();
		}
	},

	/** ****** TOOL BASE GIS ******* */
	Pan_Action : function(isActive) {
		MapAction.DeActive();
		if (isActive)
			pan_map.activate();
		else
			pan_map.deactivate();
	},

	Measure_Action : function() {
		MapAction.DeActive();
	},

	/** ******* SPLIT - MERGE - EXTENT - TRIM ************** */
	Split_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		split.layer = map.layers[indexLayer];
		split.activate();
	},
	
	/**************Chuc nang Fix_Zoom**********/
	Fix_Zoom_In : function(isActive) {
		MapAction.DeActive();
		draw_Zoom.activate();
    },  
	/****************************************************************/
	
	Merge_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		if (ctrlSelectFeatures != null)
			map.removeControl(ctrlSelectFeatures);
		ctrlSelectFeatures = new OpenLayers.Control.SelectFeature(
				map.layers[indexLayer], {
					clickout : true,
					toggle : false,
					multiple : false,
					hover : false,
					toggleKey : "ctrlKey", // ctrl key removes from selection
					multipleKey : "shiftKey", // shift key adds to selection
					box : true,
					onSelect : selectedFeature_Merge
				});
		map.addControl(ctrlSelectFeatures);
		ctrlSelectFeatures.activate();
	},
	
	/** ******* POINT - POLYLINE - POLYGON ******** */
	Add_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		indexLayer = indexLayer - 1 - map_WMS.length;
		if (isActive)
			Draw_Obj[indexLayer].activate();
		else
			Draw_Obj[indexLayer].deactivate();
	},

	Edit_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		indexLayer = indexLayer - 1 - map_WMS.length;
		if (isActive)
			Modify_Obj[indexLayer].activate();
		else
			Modify_Obj[indexLayer].deactivate();
	},

	Del_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		indexLayer = indexLayer - 1 - map_WMS.length;
		if (isActive)
			Del_Obj[indexLayer].activate();
		else
			Del_Obj[indexLayer].deactivate();
	},

	DelVertice_Action : function(isActive) {
		MapAction.DeActive();
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		indexLayer = indexLayer - 1 - map_WMS.length;
		if (isActive)
			DelVertices_Obj[indexLayer].activate();
		else
			DelVertices_Obj[indexLayer].deactivate();
	},
	/** ********************************* */
	
	/** ************* SAVE ************** */
	Save_Action : function() {
		// alert("save");
		// if (save == undefined) return;
		var i = 0;
		for (i = 0; i < Modify_Obj.length; i++) {
			if (Modify_Obj[i] == undefined)
				continue;
			if (Modify_Obj[i].feature)
				Modify_Obj[i].selectControl.unselectAll();
		}

		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		indexLayer = indexLayer - 1 - map_WMS.length;
		Save_Obj[indexLayer].save();
		bSaveEditting = true;
		
	}
/** *************************** */
};

Portlet_Type_Action = {
	None : -1,
	Default : 0, // pan
	Zoom_In : 1,
	Zoom_Out : 2,
	Fix_Zoom_In : 3,
	Fix_Zoom_Out : 4,
	Find : 5,
	Identify : 6,
	Search : 26,
	Print : 7,
	Mesaure : 8,
	Back_Extent : 9,
	Next_Extent : 10,
	Insert : 11,
	Edit : 12,
	Delete : 13,
	Split : 14,
	Merge : 15,
	Trim : 16,
	Extent : 17,
	Add_Line_Extension : 18,
	Add_Line_Extension_Angle : 18,
	Add_Line_Extension_Parallel : 20,
	Add_Line_Extension_Distance : 21,
	Add_Vertex : 22,
	Copy : 23,
	Del_Vertext : 24,
	Save : 25
};
/** ***** end function *********** */

/*******************************************************************************
 * tuannh ********* function : panel
 ******************************************************************************/
Portlet_Image = {
	// ID: "",
	Tooltip : "",
	Action : false,
	Selected : false,
	GroupIndex : 0,
	imgName : "",
	imgControl : null,
	imgSrc : "",
	imgCtrl : null, // doi tuong draw, modify hoac delete
	Type_Action : Portlet_Type_Action.Default,
	Tool_Base : false,
	Line_Tool_Extension : false,
	Polygon_Tool_Extension : false,
	divContainer : null, // bang thong tin tuy chon
	width_Img : 27, // chieu rong cua 1 anh trong panel
	finish : true, // neu la insert thi chi khi cap nhat xong moi la true

	init : function() {
	},

	swapImg : function() {
		var imgCtrl = arguments[0];
		var bIsOn = arguments[1];
		var src = imgCtrl.src;
		if (!bIsOn)
			src = src.replace('_on.png', '_off.png');
		else
			src = src.replace('_off.png', '_on.png');
		imgCtrl.src = src;
	},

	create : function() {
		var len = arguments.length;

		var optionSplit = arguments[0].split(';');
		if (optionSplit.length == 0)
			return;
		var i = 0;
		for (i = 0; i < optionSplit.length; i++) {
			var sAttr = optionSplit[i];
			sAttr = sAttr.split(':');
			if (sAttr.length != 2)
				continue;
			var sKey = trim(sAttr[0]).toLowerCase();
			var sValue = trim(sAttr[1]);
			switch (sKey) {
			case "imgname":
				this.imgName = sValue;
				break;
			case "selected":
				if (sValue == "0")
					this.Selected = false;
				else if (sValue == "false")
					this.Selected = false;
				else
					this.Selected = true;
				break;
			case "type_action":
				this.Type_Action = parseInt(sValue);
				break;
			case "group_index":
				this.GroupIndex = parseInt(sValue);
				break;
			case "ctrl":
				this.imgCtrl = sValue;
				break;
			case "tooltip":
				this.Tooltip = sValue;
				break;
			case "imgsrc":
				this.imgSrc = sValue;
				break;
			case "toolbase":
				if (sValue == "0")
					this.Tool_Base = false;
				else if (sValue == "false")
					this.Tool_Base = false;
				else
					this.Tool_Base = true;
				break;
			case "line_extension":
				if (sValue == "0")
					this.Line_Tool_Extension = false;
				else if (sValue == "false")
					this.Line_Tool_Extension = false;
				else
					this.Line_Tool_Extension = true;
				break;
			case "polygon_extension":
				if (sValue == "0")
					this.Polygon_Tool_Extension = false;
				else if (sValue == "false")
					this.Polygon_Tool_Extension = false;
				else
					this.Polygon_Tool_Extension = true;
				break;
			case "divcontainer":
				if (sValue == "null")
					this.divContainer = null;
				else
					this.divContainer = document.getElementById(sValue);
				break;
			default:
				break;
			}
		}
		if (len > 1)
			this.imgCtrl = arguments[1];

		if (this.divContainer != null)
			this.divContainer.style.display = "none";
		this.imgControl = document.createElement("img");
		this.imgControl.id = this.imgControl.name = this.imgName;
		this.imgControl.src = this.imgSrc;
		this.imgControl.style.cursor = "pointer";
		this.imgControl.title = this.imgControl.alt = this.Tooltip;
		this.finish = true;
		return this;
	}
};

Portlet_Panel = {
	divControl : null,
	divName : "",
	ulControl : null, // ul de them danh sach li -> image
	imageList_Name : null,
	imageList_Ctrl : null,
	current_ImgAction : null, // portlet image current
	current_ImgAction_Deault : null, // portlet image default - pan action
	p_index : -1, // mac dinh panel tool bar BaseGis la 0
	isExtension : false,

	visiblePanel : function(divControl) {
		if (divControl != null) {
			divControl.style.display = "block";
			divControl.style.width = "auto";
			divControl.style.height = "auto";
		}
	},

	hidePanel : function(divControl) {
		if (divControl != null)
			divControl.style.display = "none";
		var objChild = divControl.divChild;
		while (objChild != null) {
			objChild.style.display = "none";
			objChild = objChild.divChild;
		}
	},

	resetPanel : function() {
		var porletPanel = arguments[0];
		if (porletPanel.current_ImgAction != null) {
			if (porletPanel.current_ImgAction.GroupIndex > 0) {
				if (porletPanel.current_ImgAction.divContainer != null) {
					var divContainer = porletPanel.current_ImgAction.divContainer;
					divContainer.style.display = "none";
					/*
					 * var objChild = divContainer.divChild; while(objChild !=
					 * null) { objChild.style.display = "none"; objChild =
					 * divContainer.divChild; }
					 */
				}
				var imgCtrl = porletPanel.current_ImgAction.imgControl;
				Portlet_Image.swapImg(imgCtrl, false);
			}
		}
	},

	imgMouseMove : function() {
		return false;
	},

	imgMouseOut : function() {
		return false;
	},
	
	
	imgMouseDown : function() {
				    
		var indexLayer = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
		if (this.Portlet_Panel.current_ImgAction != null) {
			if (this.Portlet_Panel.current_ImgAction.GroupIndex > 0
					&& this.Portlet_Img.GroupIndex > 0) {
				if (!Panel_Current.current_ImgAction.finish) {
					alert("Chức năng hiện tại chưa thực hiện xong");
					return;
				}
				// panel cuar portlet image truoc
				if (Panel_Current != this.Portlet_Panel
						&& Panel_Current != null) {
					var divContainer = Panel_Current.current_ImgAction.divContainer;
					if (divContainer != null) {
						divContainer.style.display = "none";
						var objChild = divContainer.divChild;
						while (objChild != null) {
							objChild.style.display = "none";
							objChild = objChild.divChild;
						}
					}
				}

				if (this.Portlet_Panel.current_ImgAction.divContainer != null) {
					// panel hien tai
					var divContainer = this.Portlet_Panel.current_ImgAction.divContainer;
					divContainer.style.display = "none";
					var objChild = divContainer.divChild;
					while (objChild != null) {
						objChild.style.display = "none";
						objChild = objChild.divChild;
					}
				}
				var imgCtrl = this.Portlet_Panel.current_ImgAction.imgControl;
				Portlet_Image.swapImg(imgCtrl, false);
			}
		}
		var isActive = true;
		var i = 0;
		click_Map_Delete = false;
		if (this.Portlet_Img.GroupIndex > 0) {
			for (i = 0; i < Potrlet_Panel_Collection.length; i++) {
				var objPanel = Potrlet_Panel_Collection[i];
				if (this.Portlet_Panel.p_index == objPanel.p_index)
					continue;
				Portlet_Panel.resetPanel(objPanel);
			}

			this.Portlet_Panel.current_ImgAction = this.Portlet_Img;
			var imgCtrl = this;
			Portlet_Image.swapImg(imgCtrl, true);

			Action_Current = this.Portlet_Img.Type_Action;
			Panel_Current = this.Portlet_Panel;
			// alert(l.abc);
			if (this.Portlet_Img.divContainer != null) {
				this.Portlet_Img.divContainer.style.display = "block";
				var top = this.Portlet_Panel.divControl.offsetTop
						+ this.Portlet_Panel.divControl.offsetHeight;
				var left = map_Left + map_Width
						- this.Portlet_Img.divContainer.offsetWidth;
				this.Portlet_Img.divContainer.style.top = (top + 5) + "px";
				this.Portlet_Img.divContainer.style.left = (left - 5) + "px";
			}
			switch (this.Portlet_Img.Type_Action) {
			case Portlet_Type_Action.Default:
				MapAction.Pan_Action(isActive);
				break;
			case Portlet_Type_Action.Zoom_In:
			case Portlet_Type_Action.Zoom_Out:
				break;
			case Portlet_Type_Action.Mesaure:
				var objMeasure = document
						.getElementsByName("radio_Map_Meassure");
				for (i = 0; i < objMeasure.length; i++) {
					if (objMeasure[i].checked) {
						switch (objMeasure[i].value) {
						case "point":
							var control = measureControls["point"];
							control.activate();
							break;
						case 'line':
							var control = measureControls["line"];
							control.activate();
							break;
						case 'area':
							var control = measureControls["polygon"];
							control.activate();
						default:
							break;
						}
					}
				}
				break;
			case Portlet_Type_Action.Identify:
				alert("Identify");
				break;
			case Portlet_Type_Action.Insert:
				Panel_Current.current_ImgAction.finish = true; // = true de co
				// the chuyen
				// sang chuc
				// nang khac
				MapAction.Add_Action(isActive);
				indexLayer = indexLayer - 1 - map_WMS.length;
				Snap_Obj[indexLayer].activate();
				break;
			case Portlet_Type_Action.Edit:
				MapAction.Edit_Action(isActive);
				var objmodifyFeature = document
						.getElementsByName('modifyFeature');
				var trVertex = _getElementById("tr_Map_ModifyFeature_Vertex");
				var geometryType = Vector_Array[indexLayer - numBaseLayer].Geometry_Type;
				indexLayer = indexLayer - 1 - map_WMS.length;
				if (geometryType == "point") {
					trVertex.style.display = "none";
					if (Modify_Obj[indexLayer].mode == OpenLayers.Control.ModifyFeature.RESHAPE) {
						Modify_Obj[indexLayer].mode = OpenLayers.Control.ModifyFeature.DRAG;
					}
				} else
					trVertex.style.display = "block";
				for (i = 0; i < objmodifyFeature.length; i++) {
					if (geometryType == "point") {
						if (objmodifyFeature[i].value == 'vertex') {
							continue;
						}
					}
					switch (Modify_Obj[indexLayer].mode) {
					case OpenLayers.Control.ModifyFeature.DRAG:
						if (objmodifyFeature[i].value == 'drag') {
							objmodifyFeature[i].checked = true;
						}
						break;
					case OpenLayers.Control.ModifyFeature.RESHAPE:
						if (objmodifyFeature[i].value == 'vertex') {
							objmodifyFeature[i].checked = true;
						}
						break;
					default:
						if (objmodifyFeature[i].value == 'attribute') {
							objmodifyFeature[i].checked = true;
						}
						break;
					}
				}
				// Modify_Obj[indexLayer].createVertices = false;
				Snap_Obj[indexLayer].activate();
				break;
			case Portlet_Type_Action.Delete:
				MapAction.Del_Action(isActive);
				click_Map_Delete = true;
				break;
			case Portlet_Type_Action.Add_Line_Extension:
				break;
			case Portlet_Type_Action.Add_Line_Extension_Angle:
			case Portlet_Type_Action.Add_Line_Extension_Parallel:
			case Portlet_Type_Action.Add_Line_Extension_Distance:
			case Portlet_Type_Action.Add_Vertex:
			case Portlet_Type_Action.Copy:
				MapAction.DeActive();
				break;
			case Portlet_Type_Action.Del_Vertext:
				MapAction.DelVertice_Action(isActive);
				break;
			case Portlet_Type_Action.Split:
				MapAction.Split_Action(isActive);
				break;
			case Portlet_Type_Action.Merge:
				MapAction.Merge_Action(isActive);
				break;
			case Portlet_Type_Action.Trim:
				MapAction.DeActive();
				break;
			case Portlet_Type_Action.Extent:
				MapAction.DeActive();
				break;
			default:
				break;
			}
		} else {
			switch (this.Portlet_Img.Type_Action) {
			case Portlet_Type_Action.Back_Extent:
				if (!bAction_BackExtent)
					return;
				navExtent.previous.activate();
				navExtent.previousTrigger();
				if (navExtent.previousStack.length == 1) {
					var imgCtrl = this;
					Portlet_Image.swapImg(imgCtrl, false);
					bAction_BackExtent = false;
					if (navExtent.nextStack.length > 0)
						Portlet_Image.swapImg(p_eNextExtent.imgControl, true);
					else
						Portlet_Image.swapImg(p_eNextExtent.imgControl, false);
				} else {
					Portlet_Image.swapImg(p_eNextExtent.imgControl, true);
				}
				bAction_NextExtent = true;
				break;
			case Portlet_Type_Action.Next_Extent:
				if (!bAction_NextExtent)
					return;
				navExtent.next.activate();
				navExtent.nextTrigger();
				if (navExtent.nextStack.length == 0) {
					var imgCtrl = this;
					Portlet_Image.swapImg(imgCtrl, false);
					bAction_NextExtent = false;
					if (navExtent.previousStack.length > 1)
						Portlet_Image.swapImg(p_eBackExtent.imgControl, true);
					else
						Portlet_Image.swapImg(p_eBackExtent.imgControl, false);
				} else {
					Portlet_Image.swapImg(p_eBackExtent.imgControl, true);
				}
				bAction_BackExtent = true;
				break;

			case Portlet_Type_Action.Fix_Zoom_In:
				MapAction.Fix_Zoom_In(isActive);
				break;
			case Portlet_Type_Action.Fix_Zoom_Out:
				//MapAction.Fix_Zoom_Out(isActive);
				break;
			case Portlet_Type_Action.Find:
				alert("Find");
				break;
			case Portlet_Type_Action.Search:
				alert("Search");
				break;
			case Portlet_Type_Action.Print:
				alert("Print");
				break;				
			case Portlet_Type_Action.Save:
				Panel_Current.current_ImgAction.finish = true;
				MapAction.Save_Action();
				break;
			default:
				break;
			}
		}
	},
	
	setCurrent_ImgAction : function() {
		if (Panel_Current.current_ImgAction != null) {
			if (!Panel_Current.current_ImgAction.finish) {
				alert("Chức năng hiện tại chưa thực hiện xong");
				return false;
			}
		}
		if (Panel_Current.current_ImgAction.divContainer != null) {
			// panel hien tai
			var divContainer = Panel_Current.current_ImgAction.divContainer;
			divContainer.style.display = "none";
			var objChild = divContainer.divChild;
			while (objChild != null) {
				objChild.style.display = "none";
				objChild = objChild.divChild;
			}
		}
		/*var imgCtrl = Panel_Current.current_ImgAction.imgControl;
		Portlet_Image.swapImg(imgCtrl, false);*/
		
		for ( var i = 0; i < Potrlet_Panel_Collection.length; i++) {
			var objPortlet = Potrlet_Panel_Collection[i];
			if (objPortlet.current_ImgAction != null) {
				if (objPortlet.current_ImgAction.GroupIndex > 0) {
					if (objPortlet.current_ImgAction.divContainer != null) {
						var divContainer = objPortlet.current_ImgAction.divContainer;
						divContainer.style.display = "none";
					}
					var imgCtrl = objPortlet.current_ImgAction.imgControl;
					Portlet_Image.swapImg(imgCtrl, false);
				}
			}
		}
		var panelPortlet = arguments[0];
		Panel_Current = panelPortlet;
		if (panelPortlet.current_ImgAction != null) {
			if (panelPortlet.current_ImgAction.GroupIndex > 0) {
				var imgCtrl = panelPortlet.current_ImgAction.imgControl;
				Portlet_Image.swapImg(imgCtrl, false);
			} else {
			}
		}
		panelPortlet.current_ImgAction = arguments[1];
		var imgCtrl = panelPortlet.current_ImgAction.imgControl;
		Portlet_Image.swapImg(imgCtrl, true);
		MapAction.DeActive();
		return true;
	},
	
	addControl : function() {
		var obj = arguments[0];
		this.imageList_Name[this.imageList_Name.length] = obj.imgName;
		this.imageList_Ctrl[this.imageList_Ctrl.length] = obj;
		var objLI = document.createElement("li");
		objLI.appendChild(obj.imgControl);
		obj.imgControl.onmousedown = this.imgMouseDown;
		obj.imgControl.onmouseover = this.imgMouseMove;
		obj.imgControl.onmouseout = this.imgMouseOut;
		obj.imgControl.Portlet_Img = obj;
		obj.imgControl.Portlet_Panel = this;
		obj.imgControl.LI = objLI;
		this.ulControl.appendChild(objLI);
		if (obj.Selected) {
			this.current_ImgAction_Deault = obj;
			this.setCurrent_ImgAction(obj);
		}
	},

	addControls : function() {
		var obj = null;
		var len = arguments.length;
		var i = 0;
		for (i = 0; i < len; i++) {
			obj = arguments[i];
			this.addControl(obj);
		}
	},

	visibleControl : function() {

	},

	hideControl : function() {

	},

	removeControl : function() {

	},

	removeAllControl : function() {

	},

	
	create : function() {
		var len = arguments.length;
		var name = arguments[0];

		this.divName = name;
		this.divControl = document.getElementById(this.divName);
		if (this.divControl == null)
			this.divControl = document.createElement(this.divName);
		var objTable = document.createElement("table");
		objTable.border = "0px solid";
		var objTR = document.createElement("tr");
		// objTR.align = "center";
		var objTD = document.createElement("TD");
		objTD.id = "td_map_tdToolbarEditting";
		this.ulControl = document.createElement("ul");
		this.ulControl.className = "toolbar_Editor";

		objTD.appendChild(this.ulControl);
		objTR.appendChild(objTD);
		objTable.appendChild(objTR);
		this.divControl.appendChild(objTable);

		var i = 0;
		this.imageList_Name = new Array();
		this.imageList_Ctrl = new Array();
		// add portlet image
		for (i = 1; i < len - 2; i++) {
			// this.addControl(arguments[i]);
			var obj = arguments[i];
			this.imageList_Name[this.imageList_Name.length] = obj.imgName;
			this.imageList_Ctrl[this.imageList_Ctrl.length] = obj;
			var objLI = document.createElement("li");
			objLI.appendChild(obj.imgControl);
			obj.imgControl.onmousedown = Portlet_Panel.imgMouseDown;
			obj.imgControl.onmouseover = Portlet_Panel.imgMouseMove;
			obj.imgControl.onmouseout = Portlet_Panel.imgMouseOut;
			obj.imgControl.Portlet_Img = obj;
			obj.imgControl.Portlet_Panel = this;
			obj.imgControl.LI = objLI;
			obj.width_Img = Portlet_Image.width_Img;
			this.ulControl.appendChild(objLI);
			if (obj.Selected) {
				this.current_ImgAction_Deault = obj;
				if (this.current_ImgAction != null) {
					if (this.current_ImgAction.GroupIndex > 0) {
						var imgCtrl = this.current_ImgAction.imgControl;
						Portlet_Image.swapImg(imgCtrl, false);
					} else {
					}
				}
				this.current_ImgAction = obj;
				var imgCtrl = this.current_ImgAction.imgControl;
				Portlet_Image.swapImg(imgCtrl, true);
			}
			if (!obj.Tool_Base)
				obj.imgControl.LI.style.display = "none";
		}
		this.p_index = parseInt(arguments[len - 2]);
		this.isExtension = arguments[len - 1];
		return this;
	},
};
var Potrlet_Panel_Collection = new Array();
/** ********* end function************* */
